﻿using Microsoft.Extensions.DependencyInjection;
using NationalD3.Application.Interfaces;
using NationalD3.Application.Services;
using NationalD3.Domain.Interfaces;
using NationalD3.Infrastructure.Data.Context;
using NationalD3.Infrastructure.Data.Repositories;
using MediatR;
using NationalD3.Domain.Base;
using NationalD3.Infrastructure.Bus;
using NationalD3.Domain.Commands.Students;
using NationalD3.Domain.CommandHandlers;
using NationalD3.Domain.Events;
using NationalD3.Domain.EventHandlers;
using NationalD3.Domain.Notifications;
using NationalD3.Application.EventSourcing;
using NationalD3.Infrastructure.SMS;

namespace NationalD3.ApiServer.Extentions
{
    public class NativeInjectorSetup
    {
        public static void RegisterServices(IServiceCollection services)
        {

            // 注入 Application 应用层
            services.AddScoped<IStudentAppService, StudentAppService>();


            // 注入 Infra - Data 基础设施数据层
            services.AddScoped<IStudentRepository, StudentRepository>();
            services.AddScoped<StudentContext>();//上下文
            services.AddScoped<EventStoreSQLContext>();//时间溯源上下文
            //短息通知注册
            services.AddTransient<IShortMessage,ShortMessage>();
            // 引用包 MediatR.Extensions.Microsoft.DependencyInjection
            services.AddMediatR(typeof(Startup));

            services.AddScoped<IEventBus, InMemoryBus>();

            //注入工作单元服务
            services.AddScoped<IUnitOfWork, UnitOfWork>();

            // 注入领域命令处理相关服务
            services.AddScoped<IRequestHandler<RegisterStudentCommand, Unit>, StudentCommandHandler>();
            services.AddScoped<IRequestHandler<UpdateStudentCommand, Unit>, StudentCommandHandler>();
            services.AddScoped<IRequestHandler<DeleteStudentCommand, Unit>, StudentCommandHandler>();
            services.AddScoped<IEventStoreRepository, EventStoreSQLRepository>();
            services.AddScoped<IEventStoreService, SqlEventStoreService>();
            services.AddMemoryCache();

            //注入领域事件处理服务
            services.AddScoped<INotificationHandler<StudentRegisteredEvent>, StudentEventHandler>();


            services.AddScoped<INotificationHandler<DomainNotification>, DomainNotificationHandler>();


        }
    }
}
